#!/bin/bash
#
# Copyright 2015-2017 Adrian DC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

# === Standalone Source Helper ===
# source <(curl -Ls https://github.com/AdrianDC/android_development_shell_tools/raw/master/android_release_cleaners.rc)

# === ROM Output Cleaner ===
function outdevcl()
{
  # Usage
  if [ -z "${1}" ]; then
    echo '';
    echo ' Usage: outdevcl <devicename> (Advanced ROM output cleaner for rebuilds)';
    echo '';
    return;
  fi;

  # Header
  echo '';
  echo -en " \e[1;33moutdevcl:\e[0;33m Cleaning up ROM outputs for '${1}'\e[0m";

  # Out path
  out_device_path=./out/target/product/${1};

  # Rebuild the build properties
  if [ -f "${out_device_path}/obj/ETC/system_build_prop_intermediates/build.prop" ]; then
    rm -f "${out_device_path}/obj/ETC/system_build_prop_intermediates/build.prop";
  fi;

  # Run bootimage output cleaner
  outbootdevcl "${1}";

  # Delete build temporary outputs
  rm -rf './out/soong/.bootstrap';
  rm -rf './out/soong/.minibootstrap';

  # Delete relevant outputs
  rm -rf "${out_device_path}/"*"${1}"*".zip";
  rm -rf "${out_device_path}/"*"${1}"*".zip.md5sum";
  rm -rf "${out_device_path}/data";
  rm -rf "${out_device_path}/obj/ETC/system_build_prop_intermediates";
  rm -rf "${out_device_path}/obj/PACKAGING/target_files_intermediates";
  rm -rf "${out_device_path}/ota_temp/RECOVERY/RAMDISK";
  rm -rf "${out_device_path}/"*"ota"*".zip";
  rm -rf "${out_device_path}/system";
  rm -rf "${out_device_path}/utilities";
  rm -rf "${out_device_path}/system"*;
}

# === ROM Output Boot Cleaner ===
function outbootdevcl()
{
  # Usage
  if [ -z "${1}" ]; then
    echo '';
    echo ' Usage: outbootdevcl <devicename> (ROM output cleaner for bootimage rebuilds)';
    echo '';
    return;
  fi;

  # Header
  echo '';
  echo -e " \e[1;33moutbootdevcl:\e[0;33m Cleaning up bootimage outputs for '${1}'\e[0m";
  echo '';

  # Out path
  out_device_path=./out/target/product/${1};

  # Rebuild the default properties
  if [ -f "${out_device_path}/ota_temp/RECOVERY/RAMDISK/default.prop" ]; then
    rm -f "${out_device_path}/ota_temp/RECOVERY/RAMDISK/default.prop";
  fi;
  if [ -f "${out_device_path}/recovery/root/default.prop" ]; then
    rm -f "${out_device_path}/recovery/root/default.prop";
  fi;

  # Delete relevant outputs
  rm -rf "${out_device_path}/boot"*;
  rm -rf "${out_device_path}/combinedroot";
  rm -rf "${out_device_path}/combined"*;
  rm -rf "${out_device_path}/install";
  rm -rf "${out_device_path}/kernel";
  rm -rf "${out_device_path}/ramdisk"*;
  rm -rf "${out_device_path}/recovery";
  rm -rf "${out_device_path}/recovery"*;
  rm -rf "${out_device_path}/root";
}

# === ROM Output Sepolicies Cleaner ===
function outsepdevcl()
{
  # Usage
  if [ -z "${1}" ]; then
    echo '';
    echo ' Usage: outsepdevcl <devicename> (ROM output cleaner for sepolicies rebuilds)';
    echo '';
    return;
  fi;

  # Header
  echo '';
  echo -e " \e[1;33moutbootdevcl:\e[0;33m Cleaning up sepolicy outputs for '${1}'\e[0m";
  echo '';

  # Out path
  out_device_path=./out/target/product/${1};

  # Cleanup ramdisk files
  rm -f "${out_device_path}/root/file_contexts"*;
  rm -f "${out_device_path}/root/"*"_contexts";
  rm -f "${out_device_path}/root/sepolicy";
  rm -f "${out_device_path}/combinedroot/file_contexts"*;
  rm -f "${out_device_path}/combinedroot/"*"_contexts";
  rm -f "${out_device_path}/combinedroot/sepolicy";
  rm -f "${out_device_path}/recovery/root/file_contexts"*;
  rm -f "${out_device_path}/recovery/root/"*"_contexts";
  rm -f "${out_device_path}/recovery/root/sepolicy";
}
